package com.cloud.wyf.Controller;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http;
import com.cloud.wyf.Entity.Dto.DataDto;
import com.cloud.wyf.Entity.Middel;
import com.cloud.wyf.Entity.Part;
import com.cloud.wyf.Entity.User;
import com.cloud.wyf.Service.UserService;
import com.cloud.wyf.Util.GetTokenUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/total")
public class TotalController {

    @Autowired
    UserService userService;

    /**
     * 得到所有中层干部和普通职工数量
     * @param request
     * @return
     */
    @RequestMapping("/getData")
    public String getData(HttpServletRequest request){
        String uid = GetTokenUtil.getInfoFromToken(request.getHeader("x-access-token"), "id");
        if(!"1".equals(uid)) return "you have no power";//判断是否是管理员，是才能继续返回数据
        Map<String, Integer> directCountMap = new HashMap<>();
        Map<String, Integer> directCountMap1 = new HashMap<>();
        List<Part> parts = userService.selectAllPart();
        for (Part part : parts) {
            String direct = part.getDirectparty();
            directCountMap.put(direct, directCountMap.getOrDefault(direct, 0)+part.getHighlevl()+part.getLowlevl());
            directCountMap1.put(direct, directCountMap1.getOrDefault(direct, 0)+part.getNum());
        }
        Set<DataDto> collect = directCountMap.entrySet().stream().map(part -> {
            return new DataDto(part.getKey(),
                directCountMap.get(part.getKey()),
                directCountMap1.get(part.getKey()),userService.selectByDirectPart(part.getKey()).stream()
                    .map(Part::getPart)
                    .collect(Collectors.joining(",")));
        }).collect(Collectors.toSet());
        return JSONObject.toJSONString(collect);
    }

    /**
     * 得到所里领导
     * @param request
     * @return
     */
    @RequestMapping("/getAllLeader")
    public String getAllLeader(HttpServletRequest request){
        if(request.getHeader("x-access-token")!=null) {
            String uid = GetTokenUtil.getInfoFromToken(request.getHeader("x-access-token"), "id");
            if (!"1".equals(uid)) return "you have no power";//判断是否是管理员，是才能继续返回数据
            return JSONObject.toJSONString(userService.selectAll());
        }else {
            //User user = (User)request.getSession().getAttribute("user");
            //if(user.getRole()==1)
                return JSONObject.toJSONString(userService.selectAll());
           // else return "you have no power";
        }
    }

    /**
     * 得到所有中层干部
     * @param request
     * @return
     */
    @RequestMapping("/getAllMiddel")
    public String getAllMiddel(HttpServletRequest request){
        if(request.getHeader("x-access-token")!=null) {
            String uid = GetTokenUtil.getInfoFromToken(request.getHeader("x-access-token"), "id");
            if (!"1".equals(uid)) return "you have no power";//判断是否是管理员，是才能继续返回数据
            return JSONObject.toJSONString(userService.selectAllMi());
        }else {
           // User user = (User)request.getSession().getAttribute("user");
            //if(user.getRole()==1)
                return JSONObject.toJSONString(userService.selectAllMi());
           // else return "you have no power";
        }
    }

    /**
     * 得到所有普通职工
     * @param request
     * @return
     */
    @RequestMapping("/getAllOrdinary")
    public String getAllOrdinary(HttpServletRequest request){
        if(request.getHeader("x-access-token")!=null&&!"".equals(request.getHeader("x-access-token"))) {
            String uid = GetTokenUtil.getInfoFromToken(request.getHeader("x-access-token"), "id");
            if (!"1".equals(uid)) return "you have no power";//判断是否是管理员，是才能继续返回数据
            else return JSONObject.toJSONString(userService.selectAllOrdinary());
        }else return JSONObject.toJSONString(userService.selectAllOrdinary());
    }


}
